package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.ApplyFormMapper;
import com.ld.hpfs.service.ApplyFormService;

/**
 * 发票画面的Service实现
 * 
 * @author
 */
@Service
public class ApplyFormServiceImpl implements ApplyFormService {

	@Autowired
	private ApplyFormMapper applyFormMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ld.hpfs.service.ApplyFormService#getApplyFormList(java.util.HashMap)
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getApplyFormList(
			HashMap<String, Object> params) {
		return applyFormMapper.getApplyFormList(params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ld.hpfs.service.ApplyFormService#deleteApplyFormList(java.util.HashMap
	 * )
	 */
	@Override
	public int deleteApplyFormList(HashMap<String, Object> params) {
		int count = applyFormMapper.deleteApplyFormList(params);
		if(count > 0){
			applyFormMapper.deleteApplyFormInvoiceByFormId(params);
			applyFormMapper.deleteApplyFormInvoiceTaxByFormId(params);
			applyFormMapper.deleteApplyFormAttacheFileByFormId(params);
		}
		return count;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ld.hpfs.service.ApplyFormService#getTaxCommonData()
	 */
	@Override
	public HashMap<String, ArrayList<HashMap<String, Object>>> getTaxCommonData() {
		ArrayList<HashMap<String, Object>> result = applyFormMapper
				.getTaxCommonData();
		// ********************************************************
		// 把List分组成HashMap对象，key是DB中的CodeKey，Object就是List，List正key对应的每一条记录
		// ["abc01","abc","值1"]
		// ["abc02","abc","值2"]
		// ["XXXX01","XXXX","值X1"]
		// ["XXXX02","XXXX","值X2"]
		// 这样的List装换成
		// {
		// "abc":[{"abc01","abc","值1"},{"abc02","abc","值2"}],
		// "XXXX":[{"XXXX01","XXXX","值X1"},{"XXXX02","XXXX","值X2"}]
		// }
		// 的形式HashMap对象存储
		// ********************************************************
		if (result == null) {
			return null;
		}
		HashMap<String, ArrayList<HashMap<String, Object>>> mapCommonData = new HashMap<String, ArrayList<HashMap<String, Object>>>();

		String codeKey = "";
		for (HashMap<String, Object> map : result) {
			codeKey = map.get("codeKey").toString();
			if (!mapCommonData.containsKey(codeKey)) {
				ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();
				listData.add(map);
				mapCommonData.put(codeKey, listData);
			} else {
				mapCommonData.get(codeKey).add(map);
			}
		}
		return mapCommonData;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ld.hpfs.service.ApplyFormService#getApplyForm(java.util.HashMap)
	 */
	@Override
	public HashMap<String, Object> getApplyForm(HashMap<String, Object> params) {
		HashMap<String, Object> mapApplyForm = applyFormMapper.getApplyForm(params);
		List<HashMap<String, Object>> attacheFileList = applyFormMapper.getApplyFormAttacheFileList(params);
		if(attacheFileList != null && attacheFileList.size() > 0){
			mapApplyForm.put("applyFormAttacheFile", attacheFileList);
		}
		return mapApplyForm;
	}

	@Override
	public ArrayList<HashMap<String, Object>> getApplyFormInvoiceList(
			HashMap<String, Object> params) {
		return applyFormMapper.getApplyFormInvoiceList(params);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getApplyFormInvoiceTaxList(
			HashMap<String, Object> params) {
		return applyFormMapper.getApplyFormInvoiceTaxList(params);
	}

	@Override
	public int insertApplyFormAttacheFile(
			HashMap<String, Object> params) {
		// 插入上传附件名和remark
		return applyFormMapper.insertApplyFormAttacheFile(params);
	}

	@Override
	public int updateApplyFormAttacheFile(
			HashMap<String, Object> params) {
		// 更新上传附件remark
		return applyFormMapper.updateApplyFormAttacheFile(params);
	}
	
	@Override
	public int deleteApplyFormAttacheFile(
			HashMap<String, Object> params) {
		// 删除上传附件By formId attacheFileId
		return applyFormMapper.deleteApplyFormAttacheFile(params);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ld.hpfs.service.ApplyFormService#saveApplyForm(java.util.HashMap)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int saveApplyForm(HashMap<String, Object> params) {
		HashMap<String, Object> map = null;
		ArrayList<HashMap<String, Object>> gridItems = null;
		int retForm = 0;
		map = (HashMap<String, Object>) params.get("applyForm");
		Object applyFormState = params.get("applyFormState");
		// 插入表单
		if (map != null && map.size() > 0 && applyFormState != null) {
			// 状态0表示插入，1表示更新
			if ((Integer) applyFormState == 0) {
				// 生成fromNO
				String form_no = applyFormMapper.getMaxFormNo();
				int aaa = Integer.parseInt(form_no.substring(4, form_no.length())) + 1;
				String ttt = "00000000" + Integer.toString(aaa);
				String newFormNo = "FORM" + ttt.substring(ttt.length()- 8, ttt.length());
				params.put("form_no", newFormNo);
				// 返回刚插入的formId
				applyFormMapper.insertApplyForm(params);
				retForm = (Integer) params.get("formId");
			} else {
				retForm = applyFormMapper.updateApplyForm(params);
			}
			
			// 插入表单的Grid1一览
			applyFormMapper.deleteApplyFormInvoiceList(params);
			gridItems = (ArrayList<HashMap<String, Object>>) params.get("applyFormGrid1");
			if (gridItems != null && gridItems.size() > 0) {
				applyFormMapper.insertApplyFormInvoiceList(params);
			}
			// 插入表单的Grid2一览
			applyFormMapper.deleteApplyFormInvoiceTaxList(params);
			gridItems = (ArrayList<HashMap<String, Object>>) params.get("applyFormGrid2");
			if (gridItems != null && gridItems.size() > 0) {
				applyFormMapper.insertApplyFormInvoiceTaxList(params);
			}
		}
		return retForm;
	}

	@Override
	public int rollBackApplyForm(HashMap<String, Object> params) {
		return applyFormMapper.rollBackApplyForm(params);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getMdataCustomerList(
			HashMap<String, Object> params) {
		int count = applyFormMapper.getMdataCustomerListCount(params);
		if (count == 0) {
			return null;
		}
		params.put("pageCount", count);
		return applyFormMapper.getMdataCustomerList(params);
	}

	@Override
	public HashMap<String, Object> getMdataCustomerDetail(
			HashMap<String, Object> params) {
		return applyFormMapper.getMdataCustomerDetail(params);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getMdataCustomerAddrList(
			HashMap<String, Object> params) {
		int count = applyFormMapper.getMdataCustomerAddrListCount(params);
		if (count == 0) {
			return null;
		}
		params.put("pageCount", count);
		return applyFormMapper.getMdataCustomerAddrList(params);
	}

	@Override
	public HashMap<String, Object> getMdataCustomerAddrDetail(
			HashMap<String, Object> params) {
		return applyFormMapper.getMdataCustomerAddrDetail(params);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getSubmitStatus(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return applyFormMapper.getSubmitStatus(param);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getApplyFormColumns(
			HashMap<String, Object> params) {
		// TODO Auto-generated method stub
		return applyFormMapper.getApplyFormColumns(params);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getSubFlag(
			HashMap<String, Object> params) {
		// TODO Auto-generated method stub
		return applyFormMapper.getSubFlag(params);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getFormListFormStatus(
			HashMap<String, Object> params) {
		// TODO Auto-generated method stub
		return applyFormMapper.getFormListFormStatus(params);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getFormListCustName(
			HashMap<String, Object> params) {
		// TODO Auto-generated method stub
		return applyFormMapper.getFormListCustName(params);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getFormListFormNo(
			HashMap<String, Object> params) {
		// TODO Auto-generated method stub
		return applyFormMapper.getFormListFormNo(params);
	}

}
